package com.aisile.crowd_funding.api.controller;

import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.system.pojo.SysUser;
import com.aisile.crowd_funding.system.pojo.SysUserExample;
import com.aisile.crowd_funding.system.service.UserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @标题 UserController
 * @作者 Mr.chen
 * @创建日期 2018/12/18 11:00
 * @版本号 1.0
 **/
@Api("提供关于用户的接口描述")
@RestController
@RequestMapping("api")
public class UserController1 {

    @Autowired
    UserService userService;

    @ApiOperation("模糊查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户昵称",dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType="query")
    })
    @ApiResponse(response=List.class,code = 200,message = "成功返回")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public List<SysUser> list(String name,String username){
        SysUserExample sysUserExample = new SysUserExample();
        if(StringUtils.isNotBlank(name)){
            sysUserExample.createCriteria().andNameLike("%"+name+"%");
        }
        if(StringUtils.isNotBlank(username)){
            sysUserExample.createCriteria().andUsernameLike("%"+username+"%");
        }
        List<SysUser> sysUsers = userService.selectByExample(sysUserExample);
        return sysUsers;
    }

    @ApiOperation("保存用户")
    @PostMapping("save")
    public R save(@RequestBody SysUser sysUser){
        int insert = userService.insert(sysUser);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation("修改用户")
    @PostMapping("update")
    public R update(@RequestBody SysUser sysUser){
        int insert = userService.updateByPrimaryKey(sysUser);
        if(insert>0){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("查找用户")
    @ApiImplicitParam(value = "userId")
    @RequestMapping("user/{userId}")
    public SysUser getUser(@PathVariable Long userId){
        SysUser sysUser = userService.selectByPrimaryKey(userId);
        return sysUser;
    }
}
